package com.lifetime.service.impl;

import com.lifetime.Face.BaiduAIFace;
import com.lifetime.SetingModel.Setingmodel;
import com.lifetime.mapper.UserMapper;
import com.lifetime.model.User;
import com.lifetime.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    BaiduAIFace faceapi;
    @Autowired
    Setingmodel setingmodel;
    @Autowired
    private JavaMailSender mailSender; //框架自带的

    @Override
    public User selectLogin(String username ) {

        return (User)userMapper.selectUser(username);
    }

    @Override
    public Map<String, Object> searchface(StringBuffer imagebase64) throws IOException {
        String substring = imagebase64.substring(imagebase64.indexOf(",")+1, imagebase64.length());
        setingmodel.setImgpath(substring);
        setingmodel.setGroupID("group1");
        System.out.println(substring);
        Map map = faceapi.FaceSearch(setingmodel);
        return map;

    }

    @Async  //意思是异步调用这个方法
    @Override
    public void sendMail(String title, String content, String email) {
        try{
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("545909047@qq.com"); // 发送人的邮箱
            helper.setSubject(title); //标题
            helper.setTo(email); //发给谁  对方邮箱
            helper.setText(content,true); //内容,true支持html内容，false支持纯文本内容
            mailSender.send(message); //发送
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
